const app = getApp()

// pages/enroll/enroll.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinformationname: "",
    usersecert: "",
    usersecertconfirm: "",

    //焦点控制
    nhao: 0,
    marginbottom: "",

    //icon控制
    icon1: "",
    icon2: "",

    //checkbox信息,1为同意，0为不同意
    checked: 0
  },

  bindinput() {},

  //获得焦点
  bindfocus() {
    this.setData({
      nhao: 1,
      marginbottom: "margin-bottom: 10rpx;",
    })
  },

  //失去焦点
  bindblur(res) {
    this.setData({
      nhao: 0,
      marginbottom: "",
    })

    if (res.detail.cursor >= 6) {
      this.setData({
        icon1: "success"
      })
    }
  },

  //密码输入
  usersecert(res) {
    // const regex = new RegExp(pattern,options)
    //const regex = /^[0-9a-zA-Z@.]+$/
    // regex.text(res.detail.value)&&
    console.log(res)
    if (res.detail.value.length >= 6 && /^[a-zA-Z0-9.]*$/.test(res.detail.value)) {
      this.setData({
        icon1: "success"
      })
    } else {
      this.setData({
        icon1: "warn"
      })
    }

    if(this.data.icon2 == "success") {
      if (res.detail.value != this.data.usersecertconfirm) {
        this.setData({
          icon2: "warn"
        })
      }
    }

    if(res.detail.value.length == 0) {
      this.setData({
        icon1:""
      })
    }
  },

  //密码确认
  usersecertconfirm(res) {
    if (res.detail.value == this.data.usersecert) {
      this.setData({
        icon2: "success"
      })
    } else {
      this.setData({
        icon2: "warn"
      })
    }
  },

  //同意协议
  checkchange(res) {
    if (res.detail.value[0] == "checkbox") {
      this.setData({
        checked: 1
      })
    } else {
      this.setData({
        checked: 0
      })
    }
  },

  //相关协议跳转
  agreement() {
    wx.navigateTo({
      url: '/pages/agreement/agreement',
    })
  },

  //注册按钮跳转
  enroll() {
    const db = wx.cloud.database()
    //获取时间戳
    const newtime = Date.now() // 获取当前时间戳（以毫秒为单位）

    //判定信息是否输入正确
    if (this.data.userinformationname == "" || this.data.icon1 == "warn" || this.data.icon2 == "warn" || this.data.checked == 0) {
      wx.showToast({
        title: '请正确填写信息',
        icon: 'none',
        duration: 1500
      })
    } else {
      let sum = 0
      //查看有无相同用户名
      wx.cloud.database().collection('RegisterInformation').where({
        username: this.data.userinformationname
      }).get().then(res => {
        if (res.data.length) {
          wx.showToast({
            title: '该用户已注册',
            icon: "none",
            duration: 2000
          })
        } else {
          sum++
        }
        return this
      }).then(res => {
        console.log(res)
        if (sum == 1) {
          //将用户信息导入数据库
          db.collection('RegisterInformation').add({
            data: {
              username: this.data.userinformationname,
              usersecert: this.data.usersecert,
              personalgarde: "",
              personalprofessional: "",
              personalname: "",
              regsitertime: newtime,
              checked: this.data.checked
            },
            success: function (res) {
              console.log("我已成功");
            }
          })

          //将数据保存在本地
          const info = {
            username: this.data.userinformationname,
            userpassword: this.data.usersecert,
            checked: this.data.checked
          }
          app.globedata.userinfo = info //为了方便获取本地数据，userinfo存储了username,usersecert,checked三个数据,而且程序同样会给globedata中的username,usersecert,checked赋值

          try {
            wx.setStorage({
              key: 'userinfo',
              data: info,
              success() {
                wx.getStorage({
                  key: 'userinfo',
                  success() {
                    //展示注册成功
                    wx.showToast({
                      title: '注册成功',
                      icon: 'success',
                      duration: 2000
                    })
                    wx.navigateTo({
                      url: '/pages/userinformation/userinformation',
                    })
                  }
                })
              }
            })
          } catch (error) {
            console.log(error)
          }
        }
      })

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //云数据库初始化
    wx.cloud.init({
      env: 'cloud1-6g2tfsziad902596',
      traceUser: true
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})